package com.linkhouse.shiro;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linkhouse.domain.vo.UserVO;
import com.linkhouse.mapper.UserMapper;
import com.linkhouse.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;


public class LoginRealm extends AuthorizingRealm {

    @Resource
    private UserService userService;
    @Resource
    private UserMapper userMapper;

    // 授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("进入认证方法");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        long phone = Long.parseLong(token.getUsername());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone",phone);
        UserVO user = userMapper.selectOne(queryWrapper);
        if(user == null) {
            return null;
        }
        return new SimpleAuthenticationInfo(user,user.getPassword(), ByteSource.Util.bytes(user.getSalt()),user.getPhone().toString());
    }
}
